package com.base.common.model.response;

import com.base.common.constants.StringConstants;
import com.base.common.enums.ErrorEnum;
import com.base.common.enums.ResponseCodeEnum;
import com.base.common.model.request.CommonRequest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;


/**
 * 通用返回结果
 *
 * @author admin
 * @date 2020/5/19 AM10:15
 */
@Data
@ToString
@ApiModel(value = "JsonResponse", description = "通用返回结果")
@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonResponse<T> extends CommonResponse implements java.io.Serializable {
    /**
     * 业务结果-数量
     */
    @ApiModelProperty(notes = "业务结果-数量")
    private int count;

    /**
     * 业务结果-数据
     */
    @ApiModelProperty(notes = "业务结果-数据")
    private T data;

    public JsonResponse() {
        // Do nothing.
    }

    /**
     * 响应对象
     *
     * @param data
     * @param count
     * @param respCode
     * @param respMsg
     * @return
     */
    private static JsonResponse build(Object data, int count, String respCode, String respMsg) {
        JsonResponse response = new JsonResponse();
        response.setData(data);
        response.setCount(count);
        response.setRespCode(respCode);
        response.setRespMsg(respMsg);
        return response;
    }

    /**
     * 响应对象
     *
     * @param data
     * @param count
     * @param respCode
     * @param respMsg
     * @param request
     * @return
     */
    private static JsonResponse build(Object data, int count, String respCode, String respMsg, CommonRequest request) {
        JsonResponse response = new JsonResponse();
        response.setReqTime(request.getReqTime());
        response.setReqTransID(request.getReqTransID());

        response.setData(data);
        response.setCount(count);
        response.setRespCode(respCode);
        response.setRespMsg(respMsg);
        return response;
    }

    /**
     * 响应对象
     *
     * @param data
     * @param count
     * @param errorEnum
     * @return
     */
    private static JsonResponse build(Object data, int count, ErrorEnum errorEnum) {
        return build(data, count, errorEnum.getCode(), errorEnum.getCodeMsg());
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static JsonResponse buildSuccess(Object data) {
        return build(data, 0, ResponseCodeEnum.SUCCESS);
    }

    /**
     * 成功
     *
     * @param data
     * @param request
     * @return
     */
    public static JsonResponse buildSuccess(Object data, CommonRequest request) {
        return build(data, 0, ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getCodeMsg(), request);
    }

    /**
     * 成功
     *
     * @param data
     * @param count
     * @return
     */
    public static JsonResponse buildSuccess(Object data, int count) {
        return build(data, count, ResponseCodeEnum.SUCCESS);
    }

    /**
     * 成功
     *
     * @return
     */
    public static JsonResponse buildSuccess() {
        return build(null, 0, ResponseCodeEnum.SUCCESS);
    }

    /**
     * 失败
     *
     * @return
     */
    public static JsonResponse buildFailure() {
        return build(null, 0, ResponseCodeEnum.UNKNOWN_ERROR);
    }


    /**
     * 失败
     *
     * @param errorMsg
     * @return
     */
    public static JsonResponse buildFailure(String errorMsg) {
        return build(null, 0, ResponseCodeEnum.UNKNOWN_ERROR.getCode(), errorMsg);
    }

    /**
     * 失败
     *
     * @param layUiCodeEnum
     * @return
     */
    public static JsonResponse buildFailure(ResponseCodeEnum layUiCodeEnum) {
        return build(null, 0, layUiCodeEnum.getCode(), layUiCodeEnum.getCodeMsg());
    }

    /**
     * 失败
     *
     * @param errorCode
     * @param errorMsg
     * @return
     */
    public static JsonResponse buildFailure(String errorCode, String errorMsg) {
        return build(null, 0, errorCode, errorMsg);
    }

    /**
     * 失败
     *
     * @param errorEnum
     * @return
     */
    public static JsonResponse buildFailure(ErrorEnum errorEnum) {
        return build(null, 0, errorEnum.getCode(), errorEnum.getCodeMsg());
    }

    /**
     * 调用fallback
     *
     * @param contextPath
     * @return
     */
    public static JsonResponse buildFallBack(String contextPath, String methodName) {
        return build(null, 0, ResponseCodeEnum.FALLBACK_ERROR.getCode(), ResponseCodeEnum.FALLBACK_ERROR.getCodeMsg()
                + StringConstants.BLANK + contextPath + StringConstants.COLON + methodName);
    }

    /**
     * 判断是否成功
     *
     * @param jsonResponse
     * @return
     */
    public static Boolean checkSuccess(JsonResponse jsonResponse) {
        if (jsonResponse == null) {
            return Boolean.FALSE;
        }
        return ResponseCodeEnum.SUCCESS.getCode().equals(jsonResponse.getRespCode()) ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * 判断是否成功
     *
     * @return
     */
    public Boolean checkSuccess() {
        return ResponseCodeEnum.SUCCESS.getCode().equals(this.getRespCode()) ? Boolean.TRUE : Boolean.FALSE;
    }
}